package com.ljx.itbbs.server.user.controller;

import com.ljx.itbbs.common.domain.vo.resp.ApiResult;
import com.ljx.itbbs.server.user.domain.entity.RoleEntity;
import com.ljx.itbbs.server.user.domain.vo.req.*;
import com.ljx.itbbs.server.user.domain.vo.resp.MenuResp;
import com.ljx.itbbs.server.user.domain.vo.resp.PermsResp;
import com.ljx.itbbs.server.user.domain.vo.resp.RoleResp;
import com.ljx.itbbs.server.user.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "角色接口")
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private PermsService permsService;

    @Autowired
    private RolePermsRelationService rolePermsRelationService;

    @Autowired
    private MenuService menuService;

    @Autowired
    private RoleMenuRelationService roleMenuRelationService;

    @Autowired
    private UserRoleRelationService userRoleRelationService;

    @ApiOperation("获取角色信息")
    @GetMapping("/info")
    public ApiResult<RoleResp> getRoleInfo(@RequestParam("roleId") Long roleId){

        return ApiResult.success(roleService.getRoleInfo(roleId));
    }

    @ApiOperation("保存角色")
    @PostMapping("/save")
    public ApiResult<Void> saveRoleInfo(@Valid @RequestBody RoleSaveReq req){
        roleService.saveRoleInfo(req);

        return ApiResult.success();
    }

    @ApiOperation("更新角色")
    @PutMapping("/update")
    public ApiResult<Void> update(@Valid @RequestBody RoleUpdateReq req){
        roleService.updateRoleInfo(req);

        return ApiResult.success();
    }

    @ApiOperation("查询角色拥有的权限列表")
    @GetMapping("/perms/list")
    public ApiResult<List<PermsResp>> getRolePermsList(@RequestParam("roleId") Long roleId){

        return ApiResult.success(permsService.getByRoleId(roleId));
    }

    @ApiOperation("给角色分配权限")
    @PostMapping("/perms/save")
    public ApiResult<Void> saveRolePerms(@Valid @RequestBody RolePermsSaveReq req){
        rolePermsRelationService.saveRolePerms(req);

        return ApiResult.success();
    }

    @ApiOperation("查询角色列表")
    @GetMapping("/list")
    public ApiResult<List<RoleResp>> getRoleList(){

        return ApiResult.success(roleService.getRoleList());
    }

    @ApiOperation("获取角色后台菜单")
    @GetMapping("/menu/list")
    public ApiResult<List<MenuResp>> getRoleMenuList(@RequestParam("roleId") Long roleId){

        return ApiResult.success(roleMenuRelationService.getRoleMenuList(roleId));
    }

    @ApiOperation("保存角色后台菜单")
    @PostMapping("/menu/save")
    public ApiResult<Void> saveRoleMenu(@Valid @RequestBody RoleMenuSaveReq req){
        roleMenuRelationService.saveRoleMenu(req);

        return ApiResult.success();
    }

    @ApiOperation("获取用户角色")
    @GetMapping("/user")
    public ApiResult<List<RoleEntity>> getUserRole(@RequestParam("uid") Long uid){

        return ApiResult.success(roleService.getByUId(uid));
    }

    @ApiOperation("给用户分配角色")
    @PostMapping("/user/give")
    public ApiResult<Void> giveUserRole(@Valid @RequestBody RoleGiveReq req){
        userRoleRelationService.giveUserRole(req);

        return ApiResult.success();
    }


}
